package com.own.demo.jts;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.utils.geo.buffer.LineBufferImpl;
import com.own.demo.utils.geo.buffer.PolygonBufferImpl;
import com.own.demo.writerfile.WriterFileUtils;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Polygon;

import java.util.Arrays;
import java.util.List;

public class 剪切面2 {
    public static void main(String[] args) {
        String gpsA = "113.301514,28.350459;113.355072,28.350459;113.396271,28.350459;113.429230,28.350459;113.480042,28.350459;113.528107,28.350459;113.552139,28.350459;113.564499,28.350459;113.603638,28.350459;113.682602,28.350459;113.301514,28.350459";
        String gpsB = "113.629044,28.369795;113.481415,28.390939;113.449143,28.201095;113.629044,28.369795";
        PolygonBufferImpl polylineBuffer= (PolygonBufferImpl) PoiTypeEnums.POLYGON.getBufferUtil();



        LineBufferImpl lineBuffer= (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();

        LineString a =  (LineString)lineBuffer.geometryCoordinate(polylineBuffer.getCoordinate(gpsA));
        LineString b =  (LineString )lineBuffer.geometryCoordinate(polylineBuffer.getCoordinate(gpsB));

        Geometry c = a.symDifference(b);
        JSONObject jsonObject=dataDraw(Arrays.asList(c.getCoordinates()));
        WriterFileUtils.writerFoFile("var polylineData1 = " + JSONObject.toJSONString(jsonObject),"E:\\ownCode\\UMA\\uam\\地图工具-路网","polylineData1.js");
        System.out.println(JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
    }

    public static JSONObject dataDraw(List<Coordinate> list) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "GeometryCollection");
        String geoType = "LineString";
        jsonObject.put("features", setXYGeo(0, 0, list, geoType));
        return jsonObject;
    }

    public static JSONArray setXYGeo(int i, int j, List<Coordinate> list, String geoType) {
        JSONArray jsonArray = new JSONArray(1024);
        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
        int coordRow = coordinates.length;
        JSONArray gpsList = new JSONArray(coordRow);
        for (int h = 0; h < coordRow; h++) {
            Coordinate coordinate = coordinates[h];
            double[] gps = new double[2];
            gps[0] = coordinate.x;
            gps[1] = coordinate.y;
            gpsList.add(gps);
        }
        addLingString(jsonArray, i, j, geoType, gpsList);
        return jsonArray;
    }

    public static void addLingString(JSONArray jsonArray, int i, int j, String geoType, JSONArray gpsList) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject geoPro = new JSONObject(1);
        geoPro.put("name", i > 0 ? j + "-" + i : "area");
        geo.put("properties", geoPro);
        JSONObject objectInfo = new JSONObject();
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        jsonArray.add(geo);
    }
}
